<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\Session;
class Appointment extends Base
{
     //预约页
     //field表示字段
     public function index()
     {
        $user_id = \think\Session::get('user_id');
        $res1 = Db::name('child')
                ->where('user_id',$user_id)
                ->field(['child_id','child_name'])
                ->select();
        $this->assign("res1",$res1);
        $res2 = Db::name('vaccine')
                ->field('vaccine_id,vaccine_name')
                ->select();
        $this->assign("res2",$res2);
        $res3 = Db::name('hospital')
                ->field('hospital_id,hospital_name')
                ->select();
        $this->assign("res3",$res3);
        // dump($res1);
        // dump($res2);
        // dump($res3);
        // exit;
         return $this->fetch();
     }
     //处理预约提交信息
     public function appointment1()
     {
        $user_id = \think\Session::get('user_id');
        $request = Request::instance();
        $post = $request->post();
        $data = [
            'child_id' =>$_POST['child_id'],
            'vaccine_id' =>$_POST['vaccine_id'],
            'hospital_id' =>$_POST['hospital_id'],
            'begin_time' =>$_POST['begin_time'],
            'user_id' =>$user_id,
            'appointment_status' =>0,
            'appointment_time' =>date('Y-m-d H:i:s'),
        ];
        $res = Db::name('appointment')
                ->where('child_id',$_POST['child_id'])
                ->where( 'vaccine_id',$_POST['vaccine_id'])
                ->find();
        $count1 = Db::name('a_date')->where('hospital_id',$_POST['hospital_id'])->where('date',$_POST['begin_time'])->find();
        $count2 = Db::name('appointment')->where('hospital_id',$_POST['hospital_id'])->where('begin_time',$_POST['begin_time'])->count();
        if($count1 == null)
        {
            $this->error('此日期暂时没有开放预约');
        }
        if($count2<$count1['count'])
        {
            if($_POST['begin_time']>date('Y-m-d H:i:s'))
            {
                if($res==null)
                {
                    if(Db::name('appointment')->insert($data))
                    {
                        return $this->success('预约成功','appointment/index');
                    }
                    else
                    {
                        $this->error('失败，请重新填写信息');
                    }
                }
                else
                {
                    $this->error('已经预约过啦~');
                }
            }
            else
            {
                return $this->error('请选择今天以后的日期');
            }
        }
        else
        {
            return $this->error('此日期预约已经满');
        }        
        
     }
     //修改预约提交信息
     public function appointment2($id)
     {
        //  dump($id);
        //  exit;
        $user_id = \think\Session::get('user_id');
        $request = Request::instance();
        $post = $request->post();
        $data = [
            'child_id' =>$_POST['child_id'],
            'vaccine_id' =>$_POST['vaccine_id'],
            'hospital_id' =>$_POST['hospital_id'],
            'begin_time' =>$_POST['begin_time'],
            'user_id' =>$user_id,
            'appointment_status' =>0,
            'appointment_time' =>date('Y-m-d H:i:s'),
        ];
        // $res = Db::name('appointment')
        //         ->where('child_id',$_POST['child_id'])
        //         ->where( 'vaccine_id',$_POST['vaccine_id'])
        //         ->find();
        $count1 = Db::name('a_date')->where('hospital_id',$_POST['hospital_id'])->where('date',$_POST['begin_time'])->find();
        $count2 = Db::name('appointment')->where('hospital_id',$_POST['hospital_id'])->where('begin_time',$_POST['begin_time'])->count();
        if($count1 == null)
        {
            $this->error('此日期暂时没有开放预约');
        }
        if($count2<$count1['count'])
        {
            if($_POST['begin_time']>date('Y-m-d H:i:s'))
            {
                // if($res==null)
                // {
                    if(Db::name('appointment')->where('appointment_id',$id)->update($data))
                    {
                        return $this->success('预约成功','appointment/index');
                    }
                    else
                    {
                        $this->error('失败，请重新填写信息');
                    }
                // }
                // else
                // {
                //     $this->error('已经预约过啦~');
                // }
            }
            else
            {
                return $this->error('请选择今天以后的日期');
            }
        }
        else
        {
            return $this->error('此日期预约已经满');
        }        
        
     }
}
